module NMBattle
  module_function
  #战斗结算
  def Total(eid,battler)
    for i in $game_variables[193]
      case i[0]
      when 1 #重生
        $game_switches[32]=true
        $game_variables[212].push(eid)
      when 2 #中止战斗
        $game_switches[53]=true
        $game_variables[184]=[]
      end
    end
    q=0
    $game_variables[224]=battler.got_mirror
    for i in $game_variables[184]
      if $game_variables[224][q]!=nil
        q+=1;next
      end
      gd=$data_enemies[i].gold
      if $game_party.item_number(20)>0 #幸运金币
        gd*=2
      end
      k=0 #额外获得金钱系数
      #天赋判断
      if $game_variables[226]!=0
        k+=$game_variables[226][5]*0.01 #财富增殖
      end
      #附魔判断
      if $game_actors[1].equip_id.include?($game_variables[231]) && $game_variables[231]>0 #夜魔祭品
        if $game_variables[237]!=0
          for t in $game_variables[237]
            case t[0]
            when 12
              k+=0.02
            when 203
              k+=0.1
            end
          end
        end
      end
      gd+=(gd*k).to_i
      if $game_switches[65] #作弊模式
        gd*=2
      end
      ex=$data_enemies[i].exp
      $game_actors[$game_variables[1]+1].exp+=ex
      $game_party.gain_gold(gd)
      q+=1
    end
    #神圣劝化
    if $game_variables[184].size==1
      enemy=$data_enemies[$game_variables[184][0]]
      if enemy.maxhp>100 && !battler.got_p(138) && enemy.dex<=999
        $game_variables[234]=[] if $game_variables[234]==0
        $game_variables[234][$game_map.map_id]=$game_variables[184][0]
      end
    end
  end
  
  def soulTotal
    @actor=$game_actors[$game_variables[1]+1]
    for i in @actor.skills
      if $data_skills[i].name[0,12]=="灵魂支配"
        l=$game_variables[184].size
        for i in 0...l
          l-=1 if $game_variables[224][i]!=nil
        end
        @eq=@actor.equip_id
        if $game_variables[231]>0 && @eq.include?($game_variables[231]) #祭品附魔
          if $game_variables[237]!=0
            for i in $game_variables[237]
              case i[0]
              when 103
                l+=2
              end
            end
          end
        end
        a=@actor.soul_max
        if @eq.include?(122) || $game_party.weapon_number(122)>0
          if $game_variables[226]!=0 #灵魂化跃迁
            if $game_variables[226][20]>=1
              l+=a/100
            end
          end
        end
        $game_variables[197]=[$game_variables[197]+l,a].min
        $game_variables[197]=[$game_variables[197],0].max
      end
    end
  end
  
  #1 重生
  #2 战斗中止
  #3 恶念瞥视
  #4 次级沟壑
  #5 灵魂汲取
  #6 战鼓
  #7 净化（神符等）
  #8 毒素烙印
  #9.冰结领域解冻
  #11 分则能成
  #12 触发了真视领域
  #13 刃甲成就
  #14 猛毒缠身
  #15 虚弱之诏
  #16 否决之力
  #17 命运之笔
  #18 暗绘
  #19 凶魂
  #20 同仇敌忾
  def battleAfterEvent(event_id)
    @event_id=event_id
    @actor=$game_actors[$game_variables[1]+1]
    for i in $game_variables[193]
      case i[0]
      when 0 #中毒
        $game_variables[192]+=i[1].to_i
        $game_player.animation_id=59
      when 6 #战鼓
        $game_variables[181][74]-=1
        if $game_variables[181][74]==0
          $game_switches[159]=false
          $game_switches[56]=true
        end
      when 7
        @actor.del_all_buff
      when 8
        @actor.add_buff(5,-1,i[1],999,true)
        get_achievement(218)
      when 11 #分则能成
        @fl=i[1]
      when 13 #刃甲成就
        get_achievement(124)
      when 14 #猛毒缠身
        @actor.add_buff_ex(8,i[1],nil,nil,true)
        $game_player.animation_id=59
      when 15 #虚弱之诏
        @actor.add_buff_ex(9,i[1],nil,nil,true)
        $game_player.animation_id=77
      when 16 #否决之力
        @actor.add_buff_ex(10,i[1],nil,nil,true)
        $game_player.animation_id=84
        if @actor.get_buff_time(10)>=21
          get_achievement(127) #全境封锁
        end
      when 17 #命运之笔
        @actor.add_buff_ex(11,i[1],nil,nil,true)
        $game_player.animation_id=85
      when 18 #暗绘
        skillDarkPaint(@event_id,i[1])
        $game_map.events[@event_id].animation_id=77
      when 19 #凶魂
        @actor.add_buff(12,2,i[1],i[1])
        $game_variables[197]=@actor.soul_max
        if $game_player.animation_id==0
          $game_player.animation_id=36
        end
      when 20 #同仇敌忾
        if $game_variables[295]==0
          $game_variables[295]={}
        end
        mapid=$game_map.map_id
        if $game_variables[295][mapid]==nil
          $game_variables[295][mapid]=i[1]
        else
          $game_variables[295][mapid]+=i[1]
        end
      end
    end
  end
  
  def skillDarkPaint(eid,val) #暗绘技能处理
    mapid=$game_map.map_id
    this=$game_map.events[eid]
    $game_map.events.each_value{ |value|
      if value.name[0,6]=="怪物" && value.character_name!="" && value.id !=this.id
        vx=value.x
        if $game_switches[72]
          len=$game_variables[297]-$game_variables[296]
          if (vx - this.x).abs > len/2
            if vx>this.x
              vx-=len
            else
              vx+=len
            end
          end
        end
        if (vx-this.x).abs<=val[0].to_i/2 && (value.y-this.y).abs<=val[1].to_i/2
          if $game_variables[294][mapid]==nil
            $game_variables[294][mapid]=[]
          end
          $game_variables[294][mapid].push([value.id,val[2],val[3],val[4]])
        end
      end
    }
  end
end